package product;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class MemberInput extends JInternalFrame {
	ProductMain3 pm;
	MemberUpdate mupdate;
	Connection conn;
	PreparedStatement pstmt = null;
	MemberSelect ms;
	ResultSet rs;
	String aa;
	String str[] = { "사원", "대리", "과장", "부장" };// 콤보박스 하기 위함
	String str2[] = { "재경부", "기획부", "홍보부", "개발부", "마케팅부", "총무부" };// 부서 콤보박스 배열
	String combo = "사원";
	String team2 = "재경부";// 부서 콤보박스
	int memberid=27;//여기서 시작
	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	public JTextField code;
	private JTextField irum;
	private JTextField jumin;
	private JTextField address;
	private JLabel label_5;
	private JLabel label_6;
	private JLabel label_7;
	private JLabel label_8;
	private JLabel label_9;
	private JTextField phone;
	private JTextField email;
	private JTextField pwd;
	private JButton btnNewButton;
	private JComboBox position;
	private JComboBox team;

	public MemberInput(Connection conn, ProductMain3 p) {
		super("MemberInput", true, true, true, false);
		this.conn=conn;
		this.pm = p;	
		setBounds(450, 200, 428, 246);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getLabel_2());
		getContentPane().add(getCode());
		getContentPane().add(getIrum());
		getContentPane().add(getJumin());
		getContentPane().add(getAddress());
		getContentPane().add(getLabel_5());
		getContentPane().add(getLabel_6());
		getContentPane().add(getLabel_7());
		getContentPane().add(getLabel_8());
		getContentPane().add(getLabel_9());
		getContentPane().add(getPhone());
		getContentPane().add(getEmail());
		getContentPane().add(getPwd());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getTeam());
		getContentPane().add(getComboBox_1());
		memid();
		//code.setText("A"+memberid);//시작
	}

	public void memid(){
		String sql = "select count(memberid) from memberdata";
		try{
			PreparedStatement ptmt =conn.prepareStatement(sql);
			ResultSet rs = ptmt.executeQuery();
			while(rs.next()){
				int midnum = rs.getInt("count(memberid)");
				code.setText("A"+midnum);
			}
			rs.close();
			ptmt.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("사원번호");
			lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 13));
			lblNewLabel.setBounds(12, 10, 81, 23);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("성       명");
			label.setFont(new Font("굴림", Font.PLAIN, 13));
			label.setBounds(12, 43, 81, 23);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("주민번호");
			label_1.setFont(new Font("굴림", Font.PLAIN, 13));
			label_1.setBounds(12, 76, 81, 23);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("거  주  지");
			label_2.setFont(new Font("굴림", Font.PLAIN, 13));
			label_2.setBounds(12, 109, 81, 23);
		}
		return label_2;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setEnabled(false);

			code.setBounds(81, 10, 109, 23);
			code.setColumns(10);
		}
		return code;
	}

	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setColumns(10);
			irum.setBounds(81, 43, 109, 23);
		}
		return irum;
	}

	private JTextField getJumin() {
		if (jumin == null) {
			jumin = new JTextField();
			jumin.setColumns(10);
			jumin.setBounds(81, 76, 109, 23);
		}
		return jumin;
	}

	private JTextField getAddress() {
		if (address == null) {
			address = new JTextField();
			address.setColumns(10);
			address.setBounds(81, 109, 109, 23);
		}
		return address;
	}

	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("연  락  처");
			label_5.setFont(new Font("굴림", Font.PLAIN, 13));
			label_5.setBounds(224, 10, 66, 23);
		}
		return label_5;
	}

	private JLabel getLabel_6() {
		if (label_6 == null) {
			label_6 = new JLabel("메일주소");
			label_6.setFont(new Font("굴림", Font.PLAIN, 13));
			label_6.setBounds(12, 142, 81, 23);
		}
		return label_6;
	}

	private JLabel getLabel_7() {
		if (label_7 == null) {
			label_7 = new JLabel("부       서");
			label_7.setFont(new Font("굴림", Font.PLAIN, 13));
			label_7.setBounds(224, 43, 81, 23);
		}
		return label_7;
	}

	private JLabel getLabel_8() {
		if (label_8 == null) {
			label_8 = new JLabel("직       책");
			label_8.setFont(new Font("굴림", Font.PLAIN, 13));
			label_8.setBounds(224, 76, 81, 23);
		}
		return label_8;
	}

	private JLabel getLabel_9() {
		if (label_9 == null) {
			label_9 = new JLabel("비밀번호");
			label_9.setFont(new Font("굴림", Font.PLAIN, 13));
			label_9.setBounds(224, 109, 81, 23);
		}
		return label_9;
	}

	private JTextField getPhone() {
		if (phone == null) {
			phone = new JTextField();
			phone.setColumns(10);
			phone.setBounds(287, 10, 109, 23);
		}
		return phone;
	}

	private JTextField getEmail() {
		if (email == null) {
			email = new JTextField();
			email.setColumns(10);
			email.setBounds(81, 142, 315, 23);
		}
		return email;
	}

	private JTextField getPwd() {
		if (pwd == null) {
			pwd = new JTextField();
			pwd.setColumns(10);
			pwd.setBounds(287, 109, 109, 23);
		}
		return pwd;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("등록완료(저장)");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					input();
				}
			});
			btnNewButton.setBounds(12, 175, 384, 23);
		}
		return btnNewButton;
	}
	public void input(){
		try{

			if (irum.getText().trim().equals("")
					|| jumin.getText().trim().equals("")
					|| address.getText().trim().equals("")
					|| phone.getText().trim().equals("")
					|| email.getText().trim().equals("")

					|| pwd.getText().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "사원정보가 누락 되었습니다");
				code.requestFocus();
				return;// 입력창이 한칸이라도 비었으면 정보누락 메시지 후 텍스트창 비움
			}else{

				String c= code.getText();
				String p= pwd.getText();
				String i= irum.getText();
				String j= jumin.getText();
				String pn= phone.getText();
				String e= email.getText();
				String a= address.getText();

				String sqlid ="insert into memberdata values(?,?,?,sysdate,?,?,?,?,?,?,0,'1')";//넥발
				pstmt=conn.prepareStatement(sqlid);		
				pstmt.setString(1, c);//사번
				pstmt.setString(2, p);//비번
				pstmt.setString(3, i);//이름
				pstmt.setString(4, j);//주민
				pstmt.setString(5, team2);//부서
				pstmt.setString(6, combo);//직책
				pstmt.setString(7, pn);//폰넘버
				pstmt.setString(8,e);//이멜주소
				pstmt.setString(9,a);//거주지주소
				pstmt.executeUpdate();//입력,수정,삭제;

				pstmt.close();

				JOptionPane.showMessageDialog(null, "사원등록(저장)이 완료 되었습니다");
				
				if(ms!=null) ms.columnadd();
				memid();
				combo = "사원";
				team2 = "재경부";
				jumin.setText("");
				phone.setText("");
				irum.setText("");
				address.setText("");
				email.setText("");
				pwd.setText("");
				code.requestFocus();
			}

		}catch(Exception e){
			JOptionPane.showMessageDialog(null,  "입력한도를넘었습니다");
			e.printStackTrace();
		}
	}

	private JComboBox getComboBox_1() {
		if (position == null) {
			position = new JComboBox(str);
			position.setBounds(287, 77, 107, 21);
			position.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					if (e.getStateChange() == e.SELECTED) {
						combo = (String) (e.getItem());
					}

				}
			});

			position.setPreferredSize(new Dimension(107, 21));

		}
		return position;
	}

	private JComboBox getTeam() {
		if (team == null) {
			team = new JComboBox(str2);
			team.setBounds(287, 42, 107, 25);
			team.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent ex) {
					if (ex.getStateChange() == ex.SELECTED) {
						team2 = (String) (ex.getItem());
					}

				}
			});
			team.setPreferredSize(new Dimension(107, 25));
		}
		return team;
	}
}
